package com.cdshangpai.photoapp.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.FilterListItemBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.ViewHolder> {
    Context context;
    public String name;
    public OnFilterItemClickListener onFilterItemClickListener;
    public ArrayList<FilterBean> datas=new ArrayList<>();


    public FilterAdapter(Context context)
    {
        this.context=context;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        FilterListItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.filter_list_item,parent,false);
        View view=binding.getRoot();
        ViewHolder viewHolder=new ViewHolder(view,binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull FilterAdapter.ViewHolder holder, int position) {
        FilterBean filterBean=datas.get(position);
        holder.binding.setViewdata(filterBean);
        holder.name=this.name;
        holder.pos=position;

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public String name;
        public Integer pos;
        public FilterListItemBinding binding;
        public ViewHolder(View view,FilterListItemBinding binding)
        {
            this(view);
            this.binding=binding;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    _click(v);
                }
            });
        }
        public ViewHolder(View view)
        {
            super(view);
        }

        private final void _click(View v)
        {
            onFilterItemClickListener.onItemClick(v,name,pos);
        }

    }
}
